from apps.trff.models import Plan
from extensions.serializers import *


class PlanSerializer(BaseSerializer):
    """交易计划"""

    creator_name = CharField(source='creator.name', read_only=True, label='创建人名称')

    class Meta:
        model = Plan
        read_only_fields = ['id','creator','team', 'creator_name', 'create_time']
        fields =['number', 'remark','is_void','creator','team', *read_only_fields]

    def validate_number(self, value):
        self.validate_unique({'number': value}, message=f'编号[{value}]已存在')
        return value

    def create(self, validated_data):
        validated_data['creator'] = self.user
        validated_data['team'] = self.team

        return super().create(validated_data)


__all__ = [

    'PlanSerializer',
]
